<template>
    <div>
        <div style="margin-top: 1%">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item >首页</el-breadcrumb-item>
                <el-breadcrumb-item>采购申请单</el-breadcrumb-item>
                <el-breadcrumb-item>新增</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <el-form :model="form" :rules="rules" ref="ruleform" label-width="140px">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="日期"  prop="addtime">
                      <el-date-picker
                        :disabled="true"
                        v-model="form.addtime"
                        type="datetime"
                        placeholder="选择日期时间">
                      </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="单据编号"  prop="djbh" >
                        <el-input v-model="form.djbh" type="number" autocomplete="off"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="使用部门"  prop="sybm">
                        <el-input  v-model="form.sybm" autocomplete="off"></el-input>
                    </el-form-item>
                </el-col>
              <el-col :span="12">
                <el-form-item label="物料代码"  prop="wlzdm">
                  <el-input  v-model="form.wlzdm" type="number" autocomplete="off"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="物料名称"  prop="wlmc">
                        <el-input  v-model="form.wlmc" autocomplete="off"></el-input>
                    </el-form-item>
                </el-col>
              <el-col :span="12">
                <el-form-item label="数量"  prop="suliang">
                  <el-input v-model="form.suliang"type="number" autocomplete="off"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="到货日期"  prop="dhrq">
                      <el-date-picker
                        v-model="form.dhrq"
                        type="datetime"
                        format="yyyy-MM-dd HH:mm:ss"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        default-time="12:00:00"
                        placeholder="选择日期时间">
                      </el-date-picker>
                    </el-form-item>
                </el-col>
              <el-col :span="12">
                <el-form-item label="供应商名称"  prop="gysmc">
                  <el-input v-model="form.gysmc" autocomplete="off"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="规格型号"  prop="ggxh">
                      <el-input v-model="form.ggxh" autocomplete="off"></el-input>
                    </el-form-item>
                </el-col>
              <el-col :span="12">
                    <el-form-item label="申请人"  prop="sqr">
                      <el-input v-model="form.sqr" autocomplete="off"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="resetForm">重 置</el-button>
            <el-button type="primary" @click="handleConfine">确 定</el-button>
        </div>
    </div>
</template>

<script>
  import CgsqdApi from "../../../api/CgsqdApi";
  export default {
    name: 'index',
    data(){
      return{
        form: {
          addtime: new Date(),
          djbh: '',
          sybm: '',
          wlzdm: '',
          wlmc: '',
          suliang: '',
          dhrq: '',
          ggxh: '',
          hsl: '',
          sqr: '',
        },
        labelWidth:'100px',
        rules: {
          addtime: [
            { required: true, message: '日期', trigger: 'blur' },
          ],
          djbh: [
            { required: true, message: '单据编号', trigger: 'blur' },
          ],
          shbz: [
            { required: true, message: '审核标志', trigger: 'blur' },
          ],
          gbbz: [
            { required: true, message: '关闭标志', trigger: 'blur' },
          ],
          sybm: [
            { required: true, message: '使用部门', trigger: 'blur' },
          ],jhddh: [
            { required: true, message: '计划订单号', trigger: 'blur' },
          ],wlzdm: [
            { required: true, message: '物料长代码', trigger: 'blur' },
          ],wlmc: [
            { required: true, message: '物料名称', trigger: 'blur' },
          ],suliang: [
            { required: true, message: '数量', trigger: 'blur' },
          ],dhrq: [
            { required: true, message: '到货日期', trigger: 'blur' },
          ],hsl: [
            { required: true, message: '换算率', trigger: 'blur' },
          ],zje: [
            { required: true, message: '总金额', trigger: 'blur' },
          ],gysmc: [
            { required: true, message: '供应商名称', trigger: 'blur' },
          ],ggxh: [
            { required: true, message: '规格型号', trigger: 'blur' },
          ],sqr: [
            { required: true, message: '申请人', trigger: 'blur' },
          ],
        }
      }
    },
    methods:{

// T和Z时间转换
      changeDate(oldDate) {
        var dateee = new Date(oldDate).toJSON();//这个ie不兼容，会返回1970年的时间
        var date = new Date(+new Date(dateee) + 8 * 3600 * 1000)
          .toISOString()
          .replace(/T/g, " ")
          .replace(/\.[\d]{3}Z/, "");
        return date;
      },

      //确认添加
      handleConfine() {
        console.log(this.form)
        var validate = false;
        this.$refs['ruleform'].validate((valid) => {
          if (valid) {
            validate = true;
          } else {
            console.log('error submit!!');
            validate = false;
            return false;
          }
        });
        if (validate === false) {
          return;
        }
        // let newData = this.changeDate(this.form.dhrq)
        // let newData = this.form.dhrq

        let data = {
          djbh: this.form.djbh,//单据编号
          sybm: this.form.sybm,//使用部门
          wlzdm:this.form.wlzdm,//物料代码
          wlmc: this.form.wlmc,//物料名称
          suliang: this.form.suliang,//数量
          dhrq: this.form.dhrq,//到货日期
          gysmc:this.form.gysmc,//供应商名称
          ggxh: this.form.ggxh,//规格型号
          sqr:this.form.sqr,//申请人
        }

        CgsqdApi.add(data)
          .then(res => {
            // 返回数据
            if(res.flag){
              this.$message.success("添加成功!");
              this.form  = {
                addtime: new Date(),
                djbh: '',
                sybm: '',
                wlzdm: '',
                wlmc: '',
                suliang: '',
                dhrq: '',
                ggxh: '',
                sqr: '',
              }
            }
            this.showAddDialog = false;
          })
          .catch(err => {
            // 异常情况
          })



      },
      resetForm() {
        this.$refs['ruleform'].resetFields();
      },
    }
  };
</script>

<style scoped>

</style>
